package problem442;

import java.util.ArrayList;
import java.util.List;

//442.数组中的重复数据(解法二)
//https://leetcode.cn/problems/find-all-duplicates-in-an-array/


class Solution2 {
    public List<Integer> findDuplicates(int[] nums) {
        int n = nums.length;
        List<Integer> list = new ArrayList<>();
        for(int i = 0; i<n; i++) {
            int t = Math.abs(nums[i]);
            if(nums[t-1] < 0) {
                list.add(t);
            }else {
                nums[t-1] = -nums[t-1];
            }
        }
//        System.out.println(Arrays.toString(nums));
        return list;
    }
}


/*

1 1 2
-1 

*/